// 地图选点配置
const key = 'RJOBZ-B7RWW-Z6DRT-3PLJR-FDW3J-MFFFJ'; //使用在腾讯位置服务申请的key
const referer = 'map0418'; //调用插件的app的名称
const location = JSON.stringify({
  longitude: 113.596168,
  latitude: 34.748781,
});
const category = '出行';
const chooseLocation = requirePlugin('chooseLocation');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    longitude: 113.596168,
    latitude: 34.748781,
    destination: '',
    markers: [{
      id: 1,
      longitude: 113.596168,
      latitude: 34.748781,
      name: '河南省郑州市中原区中原西路101号',
      width: '50px',
      height: '50px',
      iconPath: '../../images/marker.png',
      customCallout: {
        anchorY: 0,
        anchorX: 0,
        display: 'BYCLICK'
      },
      label: {
        content: '中原中路',
        color: '#eb5bab',
        fontSize: '15px',
        borderWidth: '1px',
        borderColor: '#f3ae3c',
        borderRadius: '5px',
        bgColor: '#f3ae3c',
        padding: '10px'
      }
    }],
  },
  // 地图选点
  select() {
    wx.navigateTo({
      url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location + '&category=' + category
    });
  },
  // 从地图选点插件返回后，在页面的onShow生命周期函数中能够调用插件接口，取得选点结果对象
  onShow() {
    const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
    if (location) {
      const newMarkers = [{
        id: 1,
        longitude: location.longitude || null,
        latitude: location.latitude || null,
        name: location.name || null,
        width: '50px',
        height: '50px',
        iconPath: '../../images/marker.png',
        customCallout: {
          anchorY: 0,
          anchorX: 0,
          display: 'BYCLICK'
        },
        label: {
          content: '中原中路',
          color: '#eb5bab',
          fontSize: '15px',
          borderWidth: '1px',
          borderColor: '#f3ae3c',
          borderRadius: '5px',
          bgColor: '#f3ae3c',
          padding: '10px'
        }
      }]
      this.setData({
        longitude: location.longitude,
        latitude: location.latitude,
        markers: newMarkers,
        destination: location.name
      })
    }
  },
  onUnload() {
    // 页面卸载时设置插件选点数据为null，防止再次进入页面，geLocation返回的是上次选点结果
    chooseLocation.setLocation(null);
  },
  // 拉起地图列表
  onReady() {
    this.myMap = wx.createMapContext('map')
  },
  goMapList() {
    this.myMap.openMapApp({
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      destination: this.data.destination
    })
  },

})